package com.powergisol.gis.controller;

import com.alibaba.fastjson.JSONObject;
import com.powergisol.gis.service.FallBackService;
import com.powergisol.gis.service.TabDataService;
import com.powergisol.gis.vo.FeatureTypeMap;
import com.powergisol.gis.vo.GetCapabilities;
import com.powergisol.gis.vo.TabServiceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "gis")
public class MapXmlController {
    @Autowired
    private TabDataService tabDataService;

    @Autowired
    private FallBackService fallBackService;

//    @Value("${style-url}")
//    private String styleUrl;

    @GetMapping(value = "server/xml",   produces = {MediaType.TEXT_XML_VALUE})
    @ResponseBody
    public Object getWfsJson(String serviceName) {
        TabServiceVo tabService = tabDataService.getVoByName(serviceName);
        if(null == tabService){
            return null;
        }
        GetCapabilities capabilities = new GetCapabilities();
        GetCapabilities.Layer layer1 = new GetCapabilities.Layer();
        List<GetCapabilities.LayerDetail> layers = new ArrayList<> ();
        GetCapabilities.LayerDetail layerDetail = new GetCapabilities.LayerDetail();
        layerDetail.setName(tabService.getName());
        layerDetail.setTitle(tabService.getName());
        layerDetail.setQueryable("1");
        layerDetail.setOpaque("0");

        List<String> crsList = new ArrayList<> ();
        crsList.add(tabService.getServiceCoordinate());
        crsList.add("CRS:84");
        layerDetail.setCrs(crsList);

        GetCapabilities.EXGeographicBoundingBox  exGeographicBoundingBox = new GetCapabilities.EXGeographicBoundingBox();
        String dataScope = tabService.getDataScope();

        List<GetCapabilities.BoundingBox> boundingBoxList = new ArrayList<> ();
        GetCapabilities.BoundingBox boundingBox = new GetCapabilities.BoundingBox();
        if(null != dataScope ){
            String[] scopes = dataScope.split(",");
            exGeographicBoundingBox.setEastBoundLongitude(scopes[0]);
            exGeographicBoundingBox.setNorthBoundLatitude(scopes[1]);
            exGeographicBoundingBox.setSouthBoundLatitude(scopes[3]);
            exGeographicBoundingBox.setWestBoundLongitude(scopes[2]);
            boundingBox.setCRS("CRS:84");
            boundingBox.setMaxx(scopes[0]);
            boundingBox.setMaxy(scopes[1]);
            boundingBox.setMinx(scopes[2]);
            boundingBox.setMiny(scopes[3]);
            boundingBoxList.add(boundingBox);

            boundingBox = new GetCapabilities.BoundingBox();
            boundingBox.setCRS(tabService.getServiceCoordinate());
            boundingBox.setMaxx(scopes[1]);
            boundingBox.setMaxy(scopes[0]);
            boundingBox.setMinx(scopes[3]);
            boundingBox.setMiny(scopes[2]);
            boundingBoxList.add(boundingBox);
        }
        layerDetail.setExGeographicBoundingBox(exGeographicBoundingBox);
        layerDetail.setBoundingBox(boundingBoxList);
        layers.add(layerDetail);
        layer1.setLayers(layers);
        GetCapabilities.Capability capability = new GetCapabilities.Capability();
        capability.setLayer(layer1);

        capabilities.setCapability(capability);
        capabilities.setUpdateSequence("1101");
        capabilities.setVersion("1.3.0");

        return  capabilities;
    }
}
